#include "StdAfx.h"
#include "UserInterface.h"

const string UserInterface::MESSAGE[12] = {"%s is added to Tasker.", 
                "Tasks for %s are cleared.", 
                "%s is deleted from Tasker.", 
                "%s is marked as done.",
                "There are no tasks in Tasker.", 
                "invalid command: %s", 
                "%s is an invalid date.",
                "%s is an invalid integer.",
                "There is no task \"%s\" in Tasker.",
                "Task %s is not listed.",
                "%s is an invalid time",
                "No results are found for %s", 
        };

const std::string UserInterface::MESSAGE_WELCOME = "Welcome to Tasker.\nFor more information on the list of commands available, type \"help\"\nPlease enter your command below.";

UserInterface::UserInterface(void)
{
}


UserInterface::~UserInterface(void)
{
}

bool UserInterface::stringObjToStdConversion(System::String^ source, string& result)
{
        int len = ((source->Length+1)*2);
        char* temp = new char[len];

        pin_ptr<const wchar_t> wch = PtrToStringChars( source );
        bool success = wcstombs( temp, wch, len ) != -1;

        result = temp; 
        delete temp; 
        return success;
}

string UserInterface::generateWelcomeMessage(void)
{
        return MESSAGE_WELCOME;
}

string UserInterface::readUserInput(System::String^ input)      //for when multiple controls on form
{
        string command;
        if(stringObjToStdConversion(input, command))
                return trim(command);
        else
                return "";
}

void UserInterface::startingExecutor(void)
{
        Executor execute;
        execute.loadVect();
}

string UserInterface::sendingInputToExecutor(string& input)
{
        Executor execute;
        return execute.executeCommand(input);
}

string UserInterface::generateMessage(string msg, string param)
{
        MESSAGE_TYPE message_t = determineMessageType(msg);
        char buffer[255];
        sprintf_s(buffer, MESSAGE[message_t].c_str(), param.c_str());
        return buffer;
}

UserInterface::MESSAGE_TYPE UserInterface::determineMessageType(string msg)
{
        if(msg == "added") {
            return MESSAGE_TYPE::ADDED;
                } else if (msg == "deleted") {
            return MESSAGE_TYPE::DELETED;
        } else if (msg == "invalidTaskNum") {
            return MESSAGE_TYPE::INVALID_TASK_NO;
        } else if (msg == "notInt") {
            return MESSAGE_TYPE::INVALID_INT;
        } else if (msg == "isEmptyFile") {
            return MESSAGE_TYPE::EMPTY_FILE;
        } else if (msg == "invalidDate") {
            return MESSAGE_TYPE::INVALID_DATE;
        } else if (msg == "matchUnfound") {
            return MESSAGE_TYPE::MATCH_UNFOUND;
                } else if (msg == "cleared") {
                        return MESSAGE_TYPE::CLEARED;
                } else if (msg == "Done") {
                        return MESSAGE_TYPE::DONE;
                } else if (msg == "invalidTime") {
                        return MESSAGE_TYPE::INVALID_TIME;
                } else if (msg == "invalidName") {
                        return MESSAGE_TYPE::INVALID_TASK;
                } else {
            return MESSAGE_TYPE::INVALID_CMD;
                } 
}

string UserInterface::generateHelpList(void)
{
        string cmdHelp = "add [task name] -date [dd/mm/yyyy] or -starts [hhmm] or -ends [hhmm] or -detail [detail]\n";
        cmdHelp += "delete [task number]\n";
        cmdHelp += "show \n";
        cmdHelp += "show impt\n";
        cmdHelp += "show detail\n";
        cmdHelp += "show [dd/mm/yyyy]\n";
        cmdHelp += "search [part of/full task name]\n";
        cmdHelp += "clear [dd/mm/yyyy]\n";
        cmdHelp += "done [task number]\n";
        return cmdHelp;
}

inline string UserInterface::trim(const string& s, const string& delimiters) 
{
        if(!s.empty())
        {
                int tail = s.find_last_not_of(delimiters) + 1;
                int head = s.find_first_not_of(delimiters);
                if((head == string::npos) || (tail == string::npos))
                        return "";
                return s.substr(head, tail);
        }
        else
                return s;
}